package com.ericsson.resourceplanner.base;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.DateFormat;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ericsson.resourceplanner.dao.entity.Employee;
import com.ericsson.resourceplanner.exceptions.ResourcePlannerException;
import com.ericsson.resourceplanner.model.Error;
import com.ericsson.resourceplanner.utils.ResourcePlannerConstants;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;


public abstract class AbstractController {
	private static final Logger logger = LoggerFactory
			.getLogger(AbstractController.class);

	protected String toJson(Object object) {
		ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
		String jsonString = ResourcePlannerConstants.BLANK_STRING;
		try {
			if (object != null) {

				jsonString = ow.writeValueAsString(object);

			} else {
				com.ericsson.resourceplanner.model.Error error = new Error();
				error.setCause("Object Passed for Json Conversion is Null");
				error.setMessage("Something went wrong please check input values");
				jsonString = ow.writeValueAsString(object);
			}

		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			throw new ResourcePlannerException("Exception while converting Object to Json", e);
		}
		return jsonString;

	}

	protected <T> Object getObjectFromJson(String json, TypeReference<T>type) {
		ObjectMapper mapper= new ObjectMapper();
		
		Object object = null;
		if (json != null && json.length() > 0) {
			try {
				object = mapper.readValue(json, type);
			} catch (JsonParseException e) {
				throw new ResourcePlannerException(e);
			} catch (JsonMappingException e) {
				throw new ResourcePlannerException(e);
			} catch (IOException e) {
				throw new ResourcePlannerException(e);
			}
		}

		return object;
	}

	protected void writeJsonDataInResposne(HttpServletRequest request,
			HttpServletResponse response, String jsonData) {
		try {
			// response.setContentType("application/json");
			response.getWriter().write(jsonData);
		} catch (IOException e) {
			logger.error("Exception while writing json data in respone",
					jsonData);
		}
	}

	protected void writeExcelInResposne(HttpServletRequest request,
			HttpServletResponse response, Workbook workbook, String fileName) {
		try {
			ServletOutputStream outputStream = response.getOutputStream();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment;filename="
					+ fileName);
			workbook.write(outputStream);
			outputStream.flush();
		} catch (IOException e) {
			logger.error("Exception while writing Excel in respone", e);
		}
		 
	}
	
	
}
